package com.example.takeaway.api;

import com.example.takeaway.common.R;
import com.example.takeaway.model.PageVO;
import com.example.takeaway.model.dto.CategoryDTO;
import com.example.takeaway.model.dto.CategoryEditorDTO;
import com.example.takeaway.model.vo.CategoryVO;
import com.example.takeaway.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/category")
public class CategoryApi {


    @Resource
    CategoryService categoryService;

    @PostMapping
    public R<Void> add(@RequestBody CategoryDTO param) {
        categoryService.saveCategory(param);
        return R.ok();
    }

    @PutMapping
    public R<Void> update(@RequestBody CategoryEditorDTO param) {
        categoryService.updateCategory(param);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R<CategoryVO> get(@PathVariable Long id) {
        return R.ok(categoryService.queryById(id));
    }

    @DeleteMapping("/{id}")
    public R<Map<Integer, List<String>>> delete(@PathVariable Long id) {
        return R.ok(categoryService.deleteCategoryByIds(Collections.singletonList(id)));
    }

    @GetMapping("/page")
    public R<PageVO<CategoryVO>> get(@RequestParam Integer page, @RequestParam Integer size) {
        return R.ok(categoryService.queryPage(page, size));
    }

    @GetMapping("/list")
    public R<List<CategoryVO>> list() {
        return R.ok(categoryService.queryList());
    }
}
